﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using ResourcesManager;

namespace BaseControls
{
    /// <summary>
    /// Base class for any HOG game items.
    /// </summary>
    [TemplateVisualState(GroupName = "CustomStates", Name = "ItemComplete")]
    [TemplateVisualState(GroupName = "CustomStates", Name = "Normal")]
    public class HogBaseItem : BaseItem
    {
        public HogBaseItem():base()
        {
            this.MouseLeftButtonUp += new MouseButtonEventHandler(MouseClick);
            this.DefaultStyleKey = typeof(HogBaseItem);
        }        
        /// <summary>
        /// This method is represent Item logic for HOG.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Arguments</param>
        void MouseClick(object sender, MouseButtonEventArgs e)
        {
            if (this.Parent != null && this.Parent is BaseLevel)
            {                
                if(TestHit(new WriteableBitmap(this.ImageBackground,null),e.GetPosition(this)))//test Alpha channel
                {
                    BaseLevel level = this.Parent as BaseLevel;
                    base.OnItemComplete();                    
                    //Added scenario name instead of null at object sender                    
                    if (Active)
                    {
                        ItemFound animation = new ItemFound();//start animation
                        animation.Stopped += new EventHandler((obj, ev) =>
                        {
                            level.Children.Remove(this);
                        });
                        animation.Attach(this);
                    }
                    else
                        level.Children.Remove(this);
                    level.OnItemComplete(ScenarioName, sender, new EventArgs());
                    e.Handled = true;                        
                }
            }
        }
        private Image ImageBackground;
        public override void OnApplyTemplate()
        {
            Grid layout = this.GetTemplateChild("LayoutRoot") as Grid;
            ImageBackground = layout.FindName("Image") as Image;            
            base.OnApplyTemplate();
        }
        /// <summary>
        /// This class is check that user click on items. Items can have alpha channel so Bitmap is check pixel by pixel.
        /// </summary>
        /// <param name="bitmap">Bitmap to check</param>
        /// <param name="point">Points coordinate</param>
        /// <returns></returns>
        private bool TestHit(WriteableBitmap bitmap, Point point)
        {
            int offset = Convert.ToInt32(bitmap.PixelWidth*point.Y+point.X);
            if (offset < bitmap.Pixels.Length)
            {
                int intColor = bitmap.Pixels[offset];
                if (((byte)(intColor >> 0x18) & 0xff) != 255)//check alpha channel what is in first 8 bits
                    return false;
                else
                    return true;
            }
            else
                return false;
        }
        private void Refresh()
        {            
            ResourcesManager.ResourcesManager.DownloadFile(new UriEx(new Uri((string)Image, UriKind.Relative),this), (sender,stream) =>
            {
                if (stream != null)
                {
                    base.SetStream(stream);
                }
            }, null);
        }
    }
}
